package com.yunhe.control;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.yunhe.bean.Board;
import com.yunhe.bean.ShopImfor;
import com.yunhe.service.GoodsBoardService;
import com.yunhe.util.MySession;

/**
 * Servlet implementation class goodListAdd
 */
@WebServlet("/goodListAdd.do")
public class goodListAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public goodListAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse res=(HttpServletResponse) response;
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		FileItemFactory factory = new DiskFileItemFactory();//获取组件的方法
		ServletFileUpload fileup = new ServletFileUpload(factory);//获取ServletFileUpload类的对象
		String goodsname=null;
		int bid=0;
		float price=0;
		String fileName=null;
		String cunLocation=null;
		try {
			//通过parseRequest解析每个字符数据，返回类型为List,范围是FileItem(封装表单的单个字段内容)
			List<FileItem> list = fileup.parseRequest(request);
			for (FileItem fileItem : list) {
				//isFormField()判断FileItem对象封装的数据类型（普通表单字段返回true，文件表单字段返回false）
				if(fileItem.isFormField()){
					System.out.println("普通字段的名字:\t"+fileItem.getFieldName()+"普通字段的值\t"+fileItem.getString()+"普通字段的大小\t"+fileItem.getSize());
					//返回普通表单字段
					switch(fileItem.getFieldName()){
					case "goodsname":
						goodsname=fileItem.getString();
						System.out.println("商品名"+goodsname);
						break;
					case "boardid":
						bid=Integer.parseInt(fileItem.getString());
						System.out.println("版块id"+bid);
						break;
					case "price":
						price=Float.parseFloat(fileItem.getString());
						System.out.println("商品价格"+price);
						break;
					default :
						System.out.println("错误");
						break;
					}
					
				}else{
					//获取上传的文件名
					fileName = fileItem.getName();
					System.out.println("文件名"+fileName);
					//String loac ="upload"+fileName;
					//获取文件夹位置
					cunLocation ="/img/s1/"+fileName;
					System.out.println("文件位置"+cunLocation);
					/*//将上传的文件存入文件夹
					File file = new File(cunLocation);//创建一个文件
					fileItem.write(file);*/
				}
			}	
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		SqlSession se = new MySession().getSession();
		
		ShopImfor s = new ShopImfor();
		s.setBid(bid);
		s.setSname(goodsname);
		s.setSmoney(price);
		s.setPicture(cunLocation);
		List<ShopImfor> li = new GoodsBoardService().getGoodsExist(goodsname);
		if(li.size()==0) {
			se.insert("com.yun.dao.GoodsBoardDao.insertGoods",s);
			se.commit();
			List<ShopImfor> list =new GoodsBoardService().getGoodsImage(goodsname);
			response.sendRedirect("showGoodList.do");
		}else {
			response.sendRedirect("goods-list-add.jsp");
		}
	
	}

}
